package com.homework;

//定义一个Person类{name,age,job},初始化Person对象数组，有三个
//person对象，并按照age 从大到小进行排序，提示，使用冒泡排序
public class Homework01 {
    public static void main(String[] args) {
        //初始化Person对象数组，有三个person对象
        Person[] persons = new Person[3];
        persons[0] = new Person("anna",18,"JavaEE工程师");
        persons[1] = new Person("sam",49,"大数据工程师");
        persons[2] = new Person("jack",28,"PHP工程师");

        //输出当前对象数组
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i]);//默认对象的.toString()
        }

        //使用冒泡排序
        Person temp = null;
        for (int i = 0; i < persons.length - 1; i++) {
            for (int j = 0; j < persons.length - 1 -i; j++) {
                //并按照age 从大到小进行排序，如果前面的人的age < 后面的人的age,就交换
                if(persons[i].getAge() < persons[i + 1].getAge()){
                    temp = persons[i + 1];
                    persons[i + 1] = persons[i];
                    persons[i] = temp;

                }
            }
        }
        System.out.println("排序后的效果");
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i]);//默认对象的.toString()
        }
    }
}
class Person{
    private String name;
    private int age;
    private String job;

    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }
}